package controller;

import java.sql.SQLException;

import model.City;
import model.Outsider;

import db.DBConnection;
import db.DBOutsider;
import db.IFOutsider;

public class OutsiderController
{
	private static OutsiderController instance = null;

	public static OutsiderController getInstance()
	{
		if(instance == null)
			instance = new OutsiderController();

		return instance;
	}

	public int createOutsider(String name, String phoneNo, City city, boolean active, String email, int jobID) throws SQLException
	{
		int result = -3;

		try
		{	
			Outsider newOutsider = new Outsider();

			newOutsider.setName(name);
			newOutsider.setPhoneNo(phoneNo);
			newOutsider.setCity(city);
			newOutsider.setActive(active);
			newOutsider.setEmail(email);

			DBConnection.startTransaction();

			IFOutsider dbOutsider = new DBOutsider();
			result = dbOutsider.createOutsider(newOutsider, jobID);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateOutsider(int outsiderID, String name, String phoneNo, City city, boolean active, String email, int jobID) throws SQLException
	{
		int result = -3;

		try
		{	
			Outsider newOutsider = new Outsider();

			newOutsider.setOutsiderID(outsiderID);
			newOutsider.setName(name);
			newOutsider.setPhoneNo(phoneNo);
			newOutsider.setCity(city);
			newOutsider.setActive(active);
			newOutsider.setEmail(email);

			DBConnection.startTransaction();

			IFOutsider dbOutsider = new DBOutsider();
			result = dbOutsider.updateOutsider(newOutsider, jobID);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int activateOutsider(int outsiderID) throws SQLException
	{
		IFOutsider dbOutsider = new DBOutsider();

		return dbOutsider.activateOutsider(outsiderID);
	}
}